package com.xjrsoft.module.oa.controller;

import cn.hutool.core.io.FileUtil;
import com.xjrsoft.core.tool.utils.IoUtil;
import com.xjrsoft.module.base.entity.XjrBaseUser;
import com.xjrsoft.module.base.service.IXjrBaseUserService;
import com.xjrsoft.module.oa.entity.XjrOaFileinfo;
import com.xjrsoft.module.oa.service.IXjrOafileinfoService;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * Copyright 2016-2019 长沙湘北智造信息技术有限公司
 *
 * @version xbzz V2.0.28
 * @author: 湘北智造-框架开发组
 * @date:2019年12月16日
 * @url: /oafilemanage
 * @description: wps的接口回调控制器
 */
@RestController
public class WpsCallBackController{
	
	/**
	 * wps的appsecret
	 */
	@Value("${xjrsoft.global-config.wps.download_url}")
	public String download_url;
	
    @Resource
	IXjrOafileinfoService oaFileInfoService;
    @Resource
	IXjrBaseUserService userService;
	/**
     * @author 湘北智造-框架开发组
     * @date:2020年7月1日
     * @description:wps的回调方法-获取文件信息
     */
    @RequestMapping(value="/v1/3rd/file/info", method = RequestMethod.GET)
    @ResponseBody
    public Object fileInfo(@RequestParam("_w_fname") String fileName, @RequestParam("_w_fileId") String fileId, 
    				@RequestParam("_w_userId") String userId, @RequestParam("_w_operation") String operation) {
        JSONObject jsonObject = new JSONObject();
        JSONObject fileJsonObj = new JSONObject();
        JSONObject userJsonObj = new JSONObject();
        try {
			XjrOaFileinfo xjrOaFileinfo = oaFileInfoService.getById(fileId);
			if (xjrOaFileinfo != null) {
        		fileJsonObj.put("id", fileId);
        		fileJsonObj.put("name", fileName);
        		fileJsonObj.put("version", 1);
        		fileJsonObj.put("size", Long.valueOf(xjrOaFileinfo.getFileSize()));
        		fileJsonObj.put("creator", xjrOaFileinfo.getCreateUserId());
        		fileJsonObj.put("create_time", xjrOaFileinfo.getCreateDate());
        		fileJsonObj.put("modifier", "");
        		String encode = URLEncoder.encode(fileName);
        		fileJsonObj.put("download_url", download_url + "/v1/3rd/weboffice/getFile?_w_fname=" + encode + "&_w_fileId=" + fileId);
        		jsonObject.put("file", fileJsonObj);
        		// 用户信息
        		XjrBaseUser user = userService.getById(userId);
        		userJsonObj.put("id", user.getUserId());
        		userJsonObj.put("name", user.getRealName());
        		userJsonObj.put("permission", operation);
        		userJsonObj.put("avatar_url", "userModel.avatar_url");
        		jsonObject.put("user", userJsonObj);
			}
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject.toString();
    }
    
    /**
     * @author 湘北智造-框架开发组
     * @date:2020年7月1日
     * @description:wps的回调方法-获取文件
     */
	@GetMapping("/v1/3rd/weboffice/getFile")
    public ResponseEntity<byte[]> getFile(@RequestParam("_w_fname") String filename, @RequestParam("_w_fileId") String fileId) throws Exception {
		XjrOaFileinfo oaFile = oaFileInfoService.getById(fileId);
		if (oaFile != null) {
            String filePath = oaFile.getFilePath();
			String projectPath = IoUtil.getProjectPath();
			File file = FileUtil.file(projectPath + filePath);
    		@SuppressWarnings("resource")
    		InputStream inputStream = new FileInputStream(file);
    		byte[] body = new byte[inputStream.available()];
    		HttpHeaders headers=new HttpHeaders();
    		headers.add("Content-Disposition", "attachment;filename="+filename);
    		inputStream.read(body);
    		return new ResponseEntity(body, headers, HttpStatus.OK);
		} else {
			return null;
		}
    }
}
